<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 3] Data Types</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:07:20 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 3</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch03_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<H1 CLASS=chapter><A CLASS="TITLE" NAME="JLR2-CH-3">3. Data Types</A></H1>

<DIV CLASS=htmltoc>

<p>
<b>Contents:</b><br>
Primitive Types<br>
<A HREF="ch03_02.htm">Reference Types</A><BR>

<p>
</DIV>

<P CLASS=para>
A <I CLASS=emphasis>data type</I> defines the set of values that
an expression can<A NAME="CH03.TYPE1"></A>
produce or a variable can contain. The data type of a variable or
expression also defines the operations that can be performed on
the variable or expression. The type of a variable is established
by the variable's declaration, while the type of an expression is
determined by the definitions of its operators and the types of their operands.

<P CLASS=para>
Conceptually, there are two types of data in Java programs:
primitive types and reference types. The primitive types are self-contained
values that can be contained in a variable. The primitive types
are comprised of integer types, floating-point types, and the <tt CLASS=literal>boolean</tt>
type. Of these, the integer types and floating-point types are considered
arithmetic types, since arithmetic can be performed on them. Reference
types contain values that point to or identify arrays or objects.
The syntax for specifying a type is:


<p>
<img align=middle src="./figs/jlr0301.gif" alt="[Graphic: Figure from the text]" width=424 height=50 border=0>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.2">Floating-point types</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>;
<A HREF="ch05_05.htm#JLR2-CH-5-SECT-5.4.2.2">Interface method return type</A>;
<A HREF="ch05_05.htm#JLR2-CH-5-SECT-5.4.1">Interface Variables</A>;
<A HREF="ch06_01.htm#JLR2-CH-6-SECT-1.1">Local Variables</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.2.2">Method return type</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1">Primitive Types</A>;
<A HREF="ch03_02.htm#JLR2-CH-3-SECT-2">Reference Types</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.1">Variables</A>

<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-3-SECT-1">3.1 Primitive Types</A></h2>

<P CLASS=para>
<A NAME="CH03.PRIM"></A>A primitive data type represents a single value, such as a
number, a character, or a Boolean value.
Java has primitive types for arithmetic and Boolean data:


<p>
<img align=middle src="./figs/jlr0302.gif" alt="[Graphic: Figure from the text]" width=424 height=48 border=0>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1">Arithmetic Types</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.2">Boolean Type</A>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-3-SECT-1.1">Arithmetic Types</A></h3>

<P CLASS=para>
<A NAME="CH03.ARITH"></A>Unlike in C/C++, all of the arithmetic data types in Java
are specified to have representations that are independent of
the particular computer running a Java program. This guarantees
that numeric computations made by Java programs produce the same
results on all platforms.

<P CLASS=para>
There are two kinds of arithmetic types: integer and floating-point.

<P CLASS=para>
The integer types are: <tt CLASS=literal>byte</tt>,
<tt CLASS=literal>short</tt>, <tt CLASS=literal>int</tt>, <tt CLASS=literal>long</tt>,
and <tt CLASS=literal>char</tt>.
Like C/C++, character data is considered an integer type because
of its representation and because arithmetic operations can be performed
on <tt CLASS=literal>char</tt> data. Unlike C/C++, however, <tt CLASS=literal>short</tt>
<tt CLASS=literal>int</tt> and <tt CLASS=literal>long int</tt>
are not valid data types in Java. In addition, <tt CLASS=literal>signed</tt>
and <tt CLASS=literal>unsigned</tt> do not have any special meaning
in Java.

<P CLASS=para>
The floating-point data types are <tt CLASS=literal>float</tt> and
<tt CLASS=literal>double</tt>.

<P CLASS=para>
The formal definition of an arithmetic type is:


<p>
<img align=middle src="./figs/jlr0303.gif" alt="[Graphic: Figure from the text]" width=425 height=52 border=0>

<P CLASS=para>
<b>References</b>
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.1">Integer types</A>;
<A HREF="ch03_01.htm#JLR2-CH-3-SECT-1.1.2">Floating-point types</A>

<DIV CLASS=sect3>
<h4 CLASS=sect3><A CLASS="TITLE" NAME="JLR2-CH-3-SECT-1.1.1">Integer types</A></h4>

<P CLASS=para>
<A NAME="CH03.INT"></A>Java provides integer data types in a variety of sizes. Unlike
C/C++, however, the sizes of these types are part of the language
specification; they are not platform-dependent. Formally:


<p>
<img align=middle src="./figs/jlr0304.gif" alt="[Graphic: Figure from the text]" width=424 height=121 border=0>

<P CLASS=para>
The values represented
by these types are specified in Table 3-1. The representation shown
is used on all platforms and is independent of the native platform
architecture.

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="ch03-TABLE-AUTOID.1">Table 3.1: Integer Types and Their Representations</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">

<P CLASS=para>
Type</TH>
<TH ALIGN="left">

<P CLASS=para>
Representation</TH>
<TH ALIGN="left">

<P CLASS=para>
Range</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>byte</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
8-bit, signed, two's complement</TD>
<TD ALIGN="left">

<P CLASS=para>
-128 to 127</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>short</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
16-bit, signed, two's complement</TD>
<TD ALIGN="left">

<P CLASS=para>
-32768 to 32767</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>int</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
32-bit, signed, two's complement</TD>
<TD ALIGN="left">

<P CLASS=para>
-2147483648 to 2147483647</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>long</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
64-bit, signed, two's complement</TD>
<TD ALIGN="left">

<P CLASS=para>
-9223372036854775808 to 9223372036854775807</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>char</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
16-bit, unsigned, Unicode</TD>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>'\u0000'</tt> to <tt CLASS=literal>'\uffff'</tt></TD>
</TR>
</TABLE>
<P>
</DIV>
<P CLASS=para>
All of the signed integer types in Java use a two's complement
representation. Two's complement is a binary encoding for integers, which has the following properties:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>The leftmost bit is the sign bit. If the sign bit is 1, the
number is negative.

<P>
<li CLASS=listitem>Positive numbers have the usual binary representation.

<P>
<li CLASS=listitem>Negating a number involves complementing all of the bits in
the number and then adding 1 to the result.

<P>
<li CLASS=listitem>The most negative value does not have a positive equivalent.

<P>
</UL>
<P CLASS=para>
The <tt CLASS=literal>java.lang</tt> package includes the <tt CLASS=literal>Byte</tt>,
<tt CLASS=literal>Short</tt>, <tt CLASS=literal>Integer</tt>,
<tt CLASS=literal>Long</tt>, and <tt CLASS=literal>Character</tt> classes. 
These classes provide object wrappers for <tt CLASS=literal>byte</tt>,
<tt CLASS=literal>short</tt>, <tt CLASS=literal>int</tt>, <tt CLASS=literal>long</tt>,
and <tt CLASS=literal>char</tt> values, respectively. Each of 
these classes defines <tt CLASS=literal>static</tt>
<tt CLASS=literal>MIN_VALUE</tt> and <tt CLASS=literal>MAX_VALUE</tt>
variables for its minimum and maximum values.

<P CLASS=para>
Java performs all integer arithmetic using <tt CLASS=literal>int</tt>
or <tt CLASS=literal>long</tt> operations. A value that is of type <tt CLASS=literal>byte</tt>,
<tt CLASS=literal>short</tt>, or <tt CLASS=literal>char</tt> is widened to
an <tt CLASS=literal>int</tt> or a <tt CLASS=literal>long</tt> before the
arithmetic operation is performed.

<P CLASS=para>
A value of any integer type can be cast (i.e., converted)
to a value of any other integer type. Integer types, however, cannot
be cast to a <tt CLASS=literal>boolean</tt> value, nor can the <tt CLASS=literal>boolean</tt>
type be cast to an integer-type value. A value of a signed integer
type can be assigned to a value of the same or wider type without
a cast. In this case, the value is automatically widened to the
appropriate type. Table 3-2 shows whether an assignment from a particular
integer type to another integer type can be done directly or if
it requires a type cast.

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="ch03-TABLE-AUTOID.2">Table 3.2: Assignment Compatibility Between Integer Types</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">

<P CLASS=para>
To/From</TH>
<TH ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>byte</tt></TH>
<TH ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>char</tt></TH>
<TH ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>short</tt></TH>
<TH ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>int</tt></TH>
<TH ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>long</tt></TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>byte</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>char</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>short</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>int</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Cast needed</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>long</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
<TD ALIGN="left">

<P CLASS=para>
Assignable</TD>
</TR>
</TABLE>
<P>
</DIV>
<P CLASS=para>
The principle underlying the above table is that assignments
that do not lose information do not require a type cast. Assigning
a <tt CLASS=literal>short</tt> value to an <tt CLASS=literal>int</tt> without
a cast is allowed because all of the values that can be represented
by a <tt CLASS=literal>short</tt> can also be represented by <tt CLASS=literal>int</tt>.
However, assigning an <tt CLASS=literal>int</tt> value to a <tt CLASS=literal>short</tt>
is not allowed without a cast because it involves going from a 32-bit
signed quantity to a 16-bit signed quantity. Similarly, a <tt CLASS=literal>byte</tt>
value cannot be assigned to <tt CLASS=literal>char</tt> without a cast.
<tt CLASS=literal>byte</tt> is an 8-bit signed quantity, so it can represent
negative numbers. However, <tt CLASS=literal>char</tt> is a 16-bit unsigned
quantity, so it cannot represent negative numbers.

<P CLASS=para>
Java provides the following kinds of operators for integer
values:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>Comparison operators

<P>
<li CLASS=listitem>Arithmetic operators

<P>
<li CLASS=listitem>Increment and decrement operators

<P>
<li CLASS=listitem>Bitwise logical operators

<P>
</UL>
<P CLASS=para>
If all of the operands of an operator are of an integer type,
the operation is performed as an integer operation. Normally, integer
operations are performed with a precision of 32 bits. If at least
one of the operands of an integer operation is a <tt CLASS=literal>long</tt>,
however, the operation is performed with a precision of 64 bits.

<P CLASS=para>
When an integer operation overflows or underflows, there is
no indication given that the overflow or underflow occurred.

<P CLASS=para>
If the right-hand operand (the divisor) of a division or remainder
operation is 0, Java throws an <tt CLASS=literal>ArithmeticException</tt>.
Division by zero is the only circumstance that can cause an integer
operation to throw an exception.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_06.htm#JLR2-CH-4-SECT-6">Additive Operators</A>;
<A HREF="ch04_13.htm#JLR2-CH-4-SECT-13">Assignment Operators</A>;
<A HREF="ch04_10.htm#JLR2-CH-4-SECT-10">Bitwise/Logical Operators</A>;
<A HREF="ch10_02.htm">Byte</A>;
<A HREF="ch10_03.htm">Character</A>;
<A HREF="ch04_12.htm#JLR2-CH-4-SECT-12">Conditional Operator</A>;
<A HREF="ch04_09.htm#JLR2-CH-4-SECT-9">Equality Comparison Operators</A>;
<A HREF="ch04_03.htm#JLR2-CH-4-SECT-3">Increment/Decrement Operators</A>;
<A HREF="ch10_10.htm">Integer</A>;
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.3.1">Integer literals</A>;
<A HREF="ch10_11.htm">Long</A>;
<A HREF="ch04_05.htm#JLR2-CH-4-SECT-5">Multiplicative Operators</A>;
<A HREF="ch04_08.htm#JLR2-CH-4-SECT-8">Relational Comparison Operators</A>;
<A HREF="ch09_04.htm#JLR2-CH-9-SECT-4.1.1">Runtime exceptions</A>;
<A HREF="ch04_07.htm#JLR2-CH-4-SECT-7">Shift Operators</A>;
<A HREF="ch10_19.htm">Short</A>;
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4">Unary Operators</A>

</DIV>

<DIV CLASS=sect3>
<h4 CLASS=sect3><A CLASS="TITLE" NAME="JLR2-CH-3-SECT-1.1.2">Floating-point types</A></h4>

<P CLASS=para>
<A NAME="CH03.FLOAT1"></A><A NAME="CH03.FLOAT2"></A>Like C/C++, Java provides two sizes of floating-point numbers:
single precision and double precision.

<P CLASS=para>
Formally:


<p>
<img align=middle src="./figs/jlr0305.gif" alt="[Graphic: Figure from the text]" width=424 height=48 border=0>

<P CLASS=para>
Java uses the single
precision 32-bit IEEE 754 format to represent <tt CLASS=literal>float</tt>
data and the double precision 64-bit IEEE 754
format to represent <tt CLASS=literal>double</tt> data.[1]
These representations
are used on all platforms, whether or not there is native support
for the formats.  The values represented by these types are shown
in Table 3-3.

<blockquote class=footnote>
<P CLASS=para>[1] 
The IEEE
754 floating-point data representation and operations on it are
defined in <I CLASS=emphasis>IEEE Standard for Binary Floating-Point
Arithmetic</I>, ANSI/IEEE Std. 754-1985 (IEEE, New York).
The standard can be ordered by calling (908) 981-0060 or writing to
IEEE, 445 Hoes Lane, PO Box 1331, Piscataway, NJ 08855-1331, USA.
</blockquote>
<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="ch03-TABLE-AUTOID.3">Table 3.3: Floating-Point Types and Their Representations</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">

<P CLASS=para>
Type</TH>
<TH ALIGN="left">

<P CLASS=para>
Representation</TH>
<TH ALIGN="left">

<P CLASS=para>
Range</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>float</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
32-bit, IEEE 754</TD>
<TD ALIGN="left">

<P CLASS=para>
1.40239846e-45 to 3.40282347e+38</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">

<P CLASS=para>
<tt CLASS=literal>double</tt></TD>
<TD ALIGN="left">

<P CLASS=para>
64-bit, IEEE 754</TD>
<TD ALIGN="left">

<P CLASS=para>
4.94065645841246544e-324 to 1.79769313486231570e+308</TD>
</TR>
</TABLE>
<P>
</DIV>
<P CLASS=para>
Normally, non-zero <tt CLASS=literal>float</tt> values are represented
as:

<DIV CLASS=screen>
<P>
<PRE>
sign*mantissa*2^exponent
</PRE>
</DIV>

<P CLASS=para>
where <tt CLASS=literal>sign</tt> is +1 or -1, <tt CLASS=literal>mantissa</tt>
is a positive integer less than 2^24,
and <tt CLASS=literal>exponent</tt> is an integer in the inclusive range
-149 to 104.

<P CLASS=para>
Non-zero <tt CLASS=literal>double</tt> values are represented as:

<DIV CLASS=screen>
<P>
<PRE>
sign*mantissa*2^exponent
</PRE>
</DIV>

<P CLASS=para>
where <tt CLASS=literal>sign</tt> is +1 or -1, <tt CLASS=literal>mantissa</tt>
is a positive integer less than 2^53,
and <tt CLASS=literal>exponent</tt> is an integer in the inclusive range
-1045 to 1000.

<P CLASS=para>
In addition, the IEEE 754 standard defines three special values:

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>Positive infinity</I><br>
<DD>

<P CLASS=para>
This value
is produced when a <tt CLASS=literal>float</tt> or <tt CLASS=literal>double</tt>
operation overflows, or a positive value is divided by zero. Positive
infinity is by definition greater than any other <tt CLASS=literal>float</tt>
or <tt CLASS=literal>double</tt> value.

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>Negative infinity</I><br>
<DD>

<P CLASS=para>
This value
is produced when a <tt CLASS=literal>float</tt> or <tt CLASS=literal>double</tt>
operation overflows, or a negative value is divided by zero. Negative
infinity is by definition less than any other <tt CLASS=literal>float</tt>
or <tt CLASS=literal>double</tt> value.

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>Not-a-number (NaN)</I><br>
<DD>

<P CLASS=para>
This value is produced by the <tt CLASS=literal>float</tt> and
<tt CLASS=literal>double</tt>
operations such as the division of zero by zero. When NaN is one of
the operands for an operation, most arithmetic operations return
NaN as the result. Since NaN
is unordered, most comparison operators (e.g., <tt CLASS=literal>&lt;</tt>,
<tt CLASS=literal>&lt;=</tt>, <tt CLASS=literal>==</tt>, <tt CLASS=literal>&gt;=</tt>,
<tt CLASS=literal>&gt;</tt>) return <tt CLASS=literal>false</tt> when one of
their arguments is NaN. The exception is <tt CLASS=literal>!=</tt>,
which returns <tt CLASS=literal>true</tt> when one of its arguments
is NaN.</DL>
<P CLASS=para>
The <tt CLASS=literal>java.lang</tt> package includes 
<tt CLASS=literal>Float</tt> and <tt CLASS=literal>Double</tt> classes that
provide object wrappers for <tt CLASS=literal>float</tt> and <tt CLASS=literal>double</tt>
values. Each class defines the three special values as symbolic
constants: <tt CLASS=literal>POSITIVE_INFINITY</tt>, 
<tt CLASS=literal>NEGATIVE_INFINITY</tt>, and <tt CLASS=literal>NaN</tt>.
Each class also defines <tt CLASS=literal>MIN_VALUE</tt> and
<tt CLASS=literal>MAX_VALUE</tt> constants
for its minimum and maximum values.

<P CLASS=para>
Floating-point operations never throw exceptions. Operations
that overflow produce positive or negative infinity. Operations
that underflow produce positive or negative zero. Operations that
have no defined result produce not-a-number.

<P CLASS=para>
Both <tt CLASS=literal>float</tt> and <tt CLASS=literal>double</tt>
data types have distinct representations for positive and negative
zero. These values compare as equal <tt CLASS=literal>(0.0 ==</tt>
<tt CLASS=literal>-0.0)</tt>. Positive and negative zero do produce
different results for some arithmetic operations, however: <tt CLASS=literal>1.0/0.0</tt>
produces positive infinity, while <tt CLASS=literal>1.0/-0.0</tt> produces
negative infinity.

<P CLASS=para>
A <tt CLASS=literal>float</tt> value can be assigned to a <tt CLASS=literal>double</tt>
variable without using a type cast, but assigning a <tt CLASS=literal>double</tt>
value to a <tt CLASS=literal>float</tt> variable does require a cast.
Conversion from a <tt CLASS=literal>float</tt> or <tt CLASS=literal>double</tt>
value to any other data type also requires a cast. Either of the
floating-point data types can be cast to any other arithmetic type,
but they cannot be cast to <tt CLASS=literal>boolean</tt>. When a floating-point
number is cast to an integer type, it is truncated (i.e., rounded
toward zero).

<P CLASS=para>
Java provides the following kinds of operators for floating-point
values:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>Comparison operators

<P>
<li CLASS=listitem>Arithmetic operators

<P>
<li CLASS=listitem>Increment and decrement operators

<P>
</UL>
<P CLASS=para>
If any of the arguments of an operation are of a floating-point
type, the operation is performed as a floating-point operation. In
other words, any of the integer operands are converted to floating
point before the operation takes place. Floating-point operations
are normally performed with a precision of 32 bits. However, if
at least one of the operands of the operation is a <tt CLASS=literal>double</tt>,
the operation is performed with a precision of 64 bits.

<P CLASS=para>
<b>References</b>
<A HREF="ch04_06.htm#JLR2-CH-4-SECT-6">Additive Operators</A>;
<A HREF="ch04_13.htm#JLR2-CH-4-SECT-13">Assignment Operators</A>;
<A HREF="ch04_12.htm#JLR2-CH-4-SECT-12">Conditional Operator</A>;
<A HREF="ch04_09.htm#JLR2-CH-4-SECT-9">Equality Comparison Operators</A>;
<A HREF="ch10_08.htm">Double</A>;
<A HREF="ch10_09.htm">Float</A>;
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.3.2">Floating-point literals</A>;
<A HREF="ch04_03.htm#JLR2-CH-4-SECT-3">Increment/Decrement Operators</A>;
<A HREF="ch04_05.htm#JLR2-CH-4-SECT-5">Multiplicative Operators</A>;
<A HREF="ch04_08.htm#JLR2-CH-4-SECT-8">Relational Comparison Operators</A>;
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4">Unary Operators</A>

</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JLR2-CH-3-SECT-1.2">Boolean Type</A></h3>

<P CLASS=para>
<A NAME="CH03.BOOL"></A>The <tt CLASS=literal>boolean</tt> data type represents two values:
<tt CLASS=literal>true</tt> and <tt CLASS=literal>false</tt>.
These values are keywords in Java. The <tt CLASS=literal>java.lang</tt>
package includes a <tt CLASS=literal>Boolean</tt> class that provides
an object wrapper for <tt CLASS=literal>boolean</tt> values. This
<tt CLASS=literal>Boolean</tt> class
defines the constant objects <tt CLASS=literal>Boolean.TRUE</tt> and
<tt CLASS=literal>Boolean.FALSE</tt>.

<P CLASS=para>
Java provides the following kinds of operators for <tt CLASS=literal>boolean</tt>
values:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>Equality and inequality operators

<P>
<li CLASS=listitem>Boolean logical operators

<P>
</UL>
<P CLASS=para>
The following Java constructs require a <tt CLASS=literal>boolean</tt>
value to specify a condition:
<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem><tt CLASS=literal>if</tt>

<P>
<li CLASS=listitem><tt CLASS=literal>while</tt>

<P>
<li CLASS=listitem><tt CLASS=literal>for</tt>

<P>
<li CLASS=listitem><tt CLASS=literal>do</tt>

<P>
<li CLASS=listitem>The conditional operator <tt CLASS=literal>? :</tt>

<P>
</UL>
<P CLASS=para>
Unlike C/C++, any attempt to substitute a different type for
<tt CLASS=literal>boolean</tt> in these constructs is treated as an
error by Java.

<P CLASS=para>
No other data type can be cast to or from <tt CLASS=literal>boolean</tt>.
In particular, using the integer 1 to represent <tt CLASS=literal>true</tt>
and 0 to represent <tt CLASS=literal>false</tt> does not work in Java.
Though Java does not provide
conversions between <tt CLASS=literal>boolean</tt>
and other types, it is possible to provide explicit logic to accomplish
the same thing:

<DIV CLASS=screen>
<P>
<PRE>
int i;
i != 0             // This is true if i is not equal to zero
boolean b;
b ? 1 : 0          // If b is true produce 1; otherwise 0
</PRE>
</DIV>

<P CLASS=para>
<b>References</b>
<A HREF="ch10_01.htm">Boolean</A>;
<A HREF="ch04_10.htm#JLR2-CH-4-SECT-10">Bitwise/Logical Operators</A>;
<A HREF="ch02_02.htm#JLR2-CH-2-SECT-2.3.3">Boolean literals</A>;
<A HREF="ch04_04.htm#JLR2-CH-4-SECT-4.3">Boolean Negation Operator !</A>;
<A HREF="ch04_11.htm#JLR2-CH-4-SECT-11">Boolean Operators</A>;
<A HREF="ch04_12.htm#JLR2-CH-4-SECT-12">Conditional Operator</A>;
<A HREF="ch04_09.htm#JLR2-CH-4-SECT-9">Equality Comparison Operators</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.2">The do Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.3">The for Statement</A>;
<A HREF="ch06_05.htm#JLR2-CH-6-SECT-5">The if Statement</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.1">The while Statement</A>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch03_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Tokenization</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Reference Types</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
